package su.product.service.impl;

import java.util.Date;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import su.base.Constants;
import su.product.dao.ICateDAO;
import su.product.dao.IProductDAO;
import su.product.dao.IProductPriceDAO;
import su.product.entity.Cate;
import su.product.entity.Product;
import su.product.entity.ProductPrice;
import su.product.service.ProductService;
import su.tool.FunctionUtil;
import su.tool.SuDateUtils;

public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private IProductDAO productDAO;
	
	@Autowired
	private ICateDAO cateDAO;
	
	@Autowired
	private IProductPriceDAO productPriceDAO;

	protected final Log log = LogFactory.getLog(getClass());
	//============================
	public List<Product> getProductsByCate(PagerDTO s){
		List<Product> list = null;
		try {
			list = productDAO.queryByPage(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list; 
	}
	
	public Product getProductById(int id){
		Product product = null;
		try {
			product = productDAO.getById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return product;
	}
	
	public void saveProduct(Product o)throws Exception{
		o.setStatus(Constants.STATUS_ENABLE);
		if(o.getId()==0){
			//添加产品
			productDAO.insert(o);
			//修改分类的产品数量
			cateDAO.updateProductNum(o.getCateid(), true);
		}else{
			productDAO.update(o);
		}
	}
	
	public void removeProduct(int id)throws Exception{
		productDAO.remove(id);
	}
	
	public void updateProductCate(int productId,int oldCateId,int newCateId)throws Exception{
		//验证
		Cate newCate = cateDAO.getById(newCateId);
		if(newCate==null)throw new Exception("未找到id为"+newCateId+"的目标分类");
		//修改分类
		productDAO.updateProductCate(productId, oldCateId, newCateId);
		//修改原分类下产品数
		cateDAO.updateProductNum(oldCateId, false);
		//修改新分类下产品数
		cateDAO.updateProductNum(newCateId, true);
	}
	
	//================产品价格信息管理
	public List<ProductPrice> getPriceByProductId(int productId){
		List<ProductPrice> list = null;
		try {
			list = productPriceDAO.getPriceByProductId(productId);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return list;
	}
	
	public ProductPrice getProductPriceById(int id){
		ProductPrice res = null;
		try {
			res = productPriceDAO.getById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
	}

	public void saveProductPrice(ProductPrice o)throws Exception{
		o.setStatus(Constants.STATUS_ENABLE);
		if(o.getId()==0){
			//添加产品价格
			productPriceDAO.insert(o);
		}else{
			productPriceDAO.update(o);
		}
	}
	
	public void removeProductPrice(int id)throws Exception{
		productPriceDAO.remove(id);
	}
	
	public void updateAllPrice(int productId,String changeDateStr,double price)throws Exception{
		ProductPrice p = new ProductPrice();
		p.setPid(productId);
		p.setTimetoStr(changeDateStr);
		List<ProductPrice> list = productPriceDAO.getAllPrice(p);
		if(list==null || list.isEmpty())return;
		Date changeDate = SuDateUtils.getDateByFormatString(changeDateStr,"yyyy-MM-dd");
		ProductPrice pn;
		for(ProductPrice pp : list){
			pn = new ProductPrice();
			pn.setPid(productId);
			pn.setTimefrom(changeDate);
			pn.setPricefrom(FunctionUtil.sum(pp.getPricefrom(), price));
			pn.setPriceto(FunctionUtil.sum(pp.getPriceto(), price));
			pn.setPayment(pp.getPayment());
			pn.setStatus(1);
			productPriceDAO.insert(pn);
			pp.setTimeto(changeDate);
			productPriceDAO.update(pp);
		}
	}
	
}
